<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" width="800" :destroy-on-close="true">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="120px">
			<el-form-item prop="name" label="供应商名称">
				<el-input v-model="dataForm.name" placeholder="供应商名称" maxlength="140"></el-input>
			</el-form-item>
			<el-form-item prop="username" label="供联系人名称">
				<el-input v-model="dataForm.username" placeholder="联系人名称" maxlength="140"></el-input>
			</el-form-item>
			<el-form-item prop="phone" label="联系电话">
				<el-input v-model="dataForm.phone" placeholder="联系电话" maxlength="140"></el-input>
			</el-form-item>
			<el-form-item prop="address" label="地址">
				<el-input v-model="dataForm.address" placeholder="地址，多个地址请用回车换行" maxlength="5000" type="textarea" :rows="5"></el-input>
			</el-form-item>
			<el-form-item prop="remark" label="备注">
				<el-input v-model="dataForm.remark" placeholder="备注" maxlength="140"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, onMounted, nextTick } from 'vue'
import { ElMessage } from 'element-plus/es'
import { addSupplier, getSupplier } from '@/api/api'
import { timeResetDate } from '@/utils/tool'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()
const dataForm = reactive({
	id: '',
	name: '',
	username: '',
	phone: '',
	address: '',
	remark: '',
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''
	dataForm.name = ''
	dataForm.username = ''
	dataForm.phone = ''
	dataForm.address = ''
	dataForm.remark = ''
	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	// id 存在则为修改
	if (id) {
        dataForm.id = id;
		getDetail(id)
	}
}
// 获取信息
const getDetail = (id: number) => {
	getSupplier(id).then((res: any) => {
        delete res.data.created_at;
        delete res.data.updated_at;
        delete res.data.deleted_at;
		Object.assign(dataForm, res.data);
	})
}
onMounted(() => {
    // ...
})

const dataRules = ref({
	name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}
        const params:any = JSON.parse(JSON.stringify(dataForm));;
        if (dataForm.id) {
            params.id = dataForm.id;
            params.updated_at = timeResetDate();
        } else {
            delete params.id;
        }
        addSupplier(params).then((result) => {
            if (result) {
                ElMessage.success({
                    message: '操作成功',
                    duration: 500,
                    onClose: () => {
                        visible.value = false
                        emit('refreshDataList')
                    }
                })
            }
        }).catch((err) => {
            
        });
	})
}

defineExpose({
	init
})
</script>
